﻿Imports System.IO
Imports System.Drawing.Font
Public Class Tab_Controller
    'Scavenger, a visual basic web browser.
    'Copyright (C) 2011  Tetra Productions Ltd.

    'This program is free software: you can redistribute it and/or modify
    'it under the terms of the GNU General Public License as published by
    'the Free Software Foundation, either version 3 of the License, or
    '(at your option) any later version.

    'This program is distributed in the hope that it will be useful,
    'but WITHOUT ANY WARRANTY; without even the implied warranty of
    'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    'GNU General Public License for more details.

    'You should have received a copy of the GNU General Public License
    'along with this program.  If not, see <http://www.gnu.org/licenses/>.

    '//*Begin Initial Visual Controller*//
    Public Sub Add_Visuals(ByVal sender As System.Object, ByVal e As DrawItemEventArgs)
        Dim Tabs As TabControl = CType(sender, TabControl)
        Dim TabRec As Rectangle = Tabs.GetTabRect(e.Index)
        Dim Color As Color = If(e.State = DrawItemState.Selected, Color.White, Color.LightGray)
        e.Graphics.FillRectangle(New SolidBrush(Color), TabRec)
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center
        sf.FormatFlags = StringFormatFlags.NoWrap
        If Tabs.SelectedIndex = Tabs.TabCount - 1 Or Tabs.TabCount <= 1 Then
            e.Graphics.DrawImage(My.Resources.Addtab, TabRec.X + TabRec.Width + 4, TabRec.Y + 1, 32, 25)
        Else
            Dim box As Rectangle = Tabs.GetTabRect(Tabs.TabCount - 1)
            e.Graphics.DrawImage(My.Resources.Addtab, box.X + box.Width + 4, box.Y + 2, 32, 25)
        End If
        If e.State = DrawItemState.Selected Then
            Dim BTNRec As New Rectangle(TabRec.X + TabRec.Width - 19, 6, 18, 18)
            e.Graphics.DrawString("X", New Font(Scavenger.Font.FontFamily, 8, FontStyle.Regular), Brushes.Black, BTNRec, sf)
            e.Graphics.DrawImage(My.Resources.tabbg_select, TabRec)
            e.Graphics.DrawString(Tabs.TabPages(e.Index).Text, Scavenger.Font, Brushes.Black, TabRec, sf)
        Else
            Dim BTNRec As New Rectangle(TabRec.X + TabRec.Width - 19, 6, 18, 18)
            e.Graphics.DrawImage(My.Resources.tabbg, TabRec)
            e.Graphics.DrawString(Tabs.TabPages(e.Index).Text, Scavenger.Font, Brushes.White, TabRec, sf)
            e.Graphics.DrawString("X", New Font(Scavenger.Font.FontFamily, 8, FontStyle.Regular), Brushes.LightGray, BTNRec, sf)
        End If
        e.DrawFocusRectangle()
        '//*End Initial Visual Controller*//
    End Sub

    '//*Begin Mouse Event Calls*//
    '//*Changes Visuals*//
    Public Sub Mouse_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If sender Is Scavenger.TabControl1 Then
            Dim TC As TabControl = CType(sender, TabControl)
            Dim Rec As Rectangle = Scavenger.TabControl1.GetTabRect(Scavenger.TabControl1.SelectedIndex)
            Dim HitTest As New Rectangle(Rec.X + Rec.Width - 22, 1, 18, 18)
            If HitTest.Contains(New Point(e.X, e.Y)) Then
                If Not TC.TabPages.Count = 1 Then
                    TC.TabPages.Remove(TC.SelectedTab)
                End If
            End If
        Else
            Dim TC As TabControl = Scavenger.TabControl1
            Dim CT As Rectangle = TC.GetTabRect(TC.TabCount - 1)
            Dim addhit As New Rectangle(CT.X + CT.Width + 4, CT.Y + 87, 32, 25)
            If addhit.Contains(New Point(e.X, e.Y)) Then
                Scavenger.AddTab("", TC)
                TC.SelectedTab.Text = "New Tab"
                Scavenger.Refresh()
            End If
        End If
    End Sub
    Public Sub Mouse_Hover(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If sender Is Scavenger.TabControl1 Then
            Dim TC As TabControl = CType(sender, TabControl)
            Dim Rec As Rectangle = TC.GetTabRect(TC.SelectedIndex)
            Dim BTNRec As New Rectangle(Rec.X + Rec.Width - 19, 6, 18, 18)
            Dim sf As New StringFormat
            sf.Alignment = StringAlignment.Center
            sf.LineAlignment = StringAlignment.Center
            sf.FormatFlags = StringFormatFlags.NoWrap
            Dim Hover_Hit As New Rectangle(Rec.X + Rec.Width - 22, 1, 18, 18)
            If Hover_Hit.Contains(New Point(e.X, e.Y)) Then
                TC.CreateGraphics.DrawString("X", New Font(Scavenger.Font.FontFamily, 8, FontStyle.Regular), Brushes.Gray, BTNRec, sf)
            Else
                TC.CreateGraphics.DrawString("X", New Font(Scavenger.Font.FontFamily, 8, FontStyle.Regular), Brushes.Black, BTNRec, sf)
            End If
        Else
            '/Add Tab button hover controller
            Dim TC As TabControl = Scavenger.TabControl1
            Dim CT As Rectangle = TC.GetTabRect(TC.TabCount - 1)
            Dim addhit As New Rectangle(CT.X + CT.Width + 4, CT.Y + 87, 32, 25)
            If addhit.Contains(New Point(e.X, e.Y)) Then
                TC.CreateGraphics.DrawImage(My.Resources.Addtab_Hover, CT.X + CT.Width + 4, CT.Y + 1, 32, 25)
            Else
                TC.CreateGraphics.DrawImage(My.Resources.Addtab, CT.X + CT.Width + 4, CT.Y + 1, 32, 25)
            End If
        End If
    End Sub
    Public Sub Mouse_Down(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim TC As TabControl = Scavenger.TabControl1
        Dim Rec As Rectangle = TC.GetTabRect(TC.SelectedIndex)
        Dim BTNRec As New Rectangle(Rec.X + Rec.Width - 19, 6, 18, 18)
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center
        sf.FormatFlags = StringFormatFlags.NoWrap
        Dim HitTest As New Rectangle(Rec.X + Rec.Width - 22, 1, 18, 18)
        If HitTest.Contains(New Point(e.X, e.Y)) Then
            TC.CreateGraphics.DrawString("X", New Font(Scavenger.Font.FontFamily, 8, FontStyle.Regular), Brushes.Red, BTNRec, sf)
        Else
            TC.CreateGraphics.DrawString("X", New Font(Scavenger.Font.FontFamily, 8, FontStyle.Regular), Brushes.Black, BTNRec, sf)
        End If
    End Sub
    Public Sub Mouse_Up(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim TC As TabControl = CType(sender, TabControl)
        Dim Rec As Rectangle = TC.GetTabRect(Scavenger.TabControl1.SelectedIndex)
        Dim BTNRec As New Rectangle(Rec.X + Rec.Width - 19, 6, 18, 18)
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center
        sf.FormatFlags = StringFormatFlags.NoWrap
        Scavenger.TabControl1.CreateGraphics.DrawString("X", New Font(Scavenger.Font.FontFamily, 8, FontStyle.Regular), Brushes.Black, BTNRec, sf)
    End Sub
    Public Sub Mouse_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '/Add Tab Button de-hover controller
        Dim TC As TabControl = Scavenger.TabControl1
        Dim CT As Rectangle = TC.GetTabRect(TC.TabCount - 1)
        Dim BTNRec As New Rectangle(CT.X + CT.Width - 19, 6, 18, 18)
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center
        sf.FormatFlags = StringFormatFlags.NoWrap
        TC.CreateGraphics.DrawImage(My.Resources.Addtab, CT.X + CT.Width + 4, CT.Y + 1, 32, 25)
        TC.CreateGraphics.DrawString("X", New Font(Scavenger.Font.FontFamily, 8, FontStyle.Regular), Brushes.Black, BTNRec, sf)
        '//*End Mouse Call Events*//
    End Sub
End Class
